package front.hotel.action;

import hotel.service.IBedTypeService;
import hotel.service.IHotelOrderService;
import hotel.service.IHotelService;
import hotel.service.IRoomStyleService;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import common.action.BaseAction;
import common.pojo.HotelOrder;
import common.pojo.RoomStyle;

@Scope("prototype")
@Controller("hotelFrontAction")
public class HotelBuyAction extends BaseAction{
	

	private static final long serialVersionUID = 1L;
	/**
	 * 房间剩余
	 */
	public String getRoomLeft() throws Exception {
		int id=Integer.parseInt(request.getParameter("id"));
		checkinTime=request.getParameter("checkinTime");
		checkoutTime=request.getParameter("checkoutTime");
		//入住时间
		Timestamp startTime=Timestamp.valueOf(checkinTime+" 14:00:00");
		//退房时间
		Timestamp endTime=Timestamp.valueOf(checkoutTime+" 12:00:00");
		roomInfo = hotelService.findRoom(id,startTime,endTime);
		return "hotelcart";
	}
	
	/**
	 * 房间预订
	 */
	public String hotelBuy(){
		int id=Integer.parseInt(request.getParameter("id"));
		roomInfo=roomService.findInfoById(id);
		roomInfo.setBedTypeName(bedService.findInfoById(roomInfo.getBedType()).getName());
		return "hotelcart";		
	}
	
	public void getLeft() throws IOException{
		int id=Integer.parseInt(request.getParameter("id"));
		checkinTime=request.getParameter("checkinTime");
		checkoutTime=request.getParameter("checkoutTime");
		//入住时间
		Timestamp startTime=Timestamp.valueOf(checkinTime+" 14:00:00");
		//退房时间
		Timestamp endTime=Timestamp.valueOf(checkoutTime+" 12:00:00");
		int num=hotelService.getLeftNum(id,startTime,endTime);
		response.getWriter().write("{\"num\":"+num+"}");
	}
	/**
	 * 提交订单
	 */
	public void hotelBuyConfirm() throws Exception{
		
		String startTime=request.getParameter("startTime");
		String endTime=request.getParameter("endTime");
		Timestamp checkinTime=Timestamp.valueOf(startTime+" 14:00:00");
		Timestamp checkoutTime=Timestamp.valueOf(endTime+" 12:00:00");
		//判断提交订单时房间有无剩余
		if(hotelService.getLeftNum(order.getRoomStyleId(),checkinTime,checkoutTime)>=order.getTotalNum()){
		int memberId=(Integer)session.getAttribute("memberId");
		Double totalMoney=getTotalPrice(startTime, endTime, order.getPrice());
		code=getCode(memberId);	
		order.setCheckinTime(checkinTime);
		order.setCheckoutTime(checkoutTime);
		order.setCode(code);
		order.setMemberId(memberId);
		order.setTotalMoney(totalMoney);
		order.setRoomName(roomService.findInfoById(order.getRoomStyleId()).getName());
		orderService.save(order);
		response.sendRedirect("getOrderByCode.action?code="+code);
		}else{
			response.sendRedirect("cartFail.action");
		}
	} 
	
	public String cartFail(){
		return "cartfail";
	}
	
//	按照订单号得到订单详情
	public String getOrderByCode(){
		String orderCode=request.getParameter("code");
		order=orderService.findByCode(orderCode);
		return "cartverify";
	}
//按照日期和房间数量计算总价格
	public Double getTotalPrice(String startTime,String endTime,Double price) throws Exception{
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		long day=(df.parse(endTime).getTime()-df.parse(startTime).getTime())/(24*60*60*1000); 
		return day*price;
	}
//	获取订单号
	public String getCode(int memberId) {
		SimpleDateFormat df=new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String code=df.format(new Date());
		//获取会员号后两位，不足则补0
		int temp=memberId%100;
		return temp<10?code +"0"+temp:code+temp;
	}
	
	
	@Resource
	private IRoomStyleService roomService;
	@Resource
	private IBedTypeService bedService;
	@Resource
	private IHotelOrderService orderService;
	@Resource
	private IHotelService hotelService;
	
	private RoomStyle roomInfo;
	private HotelOrder order;
	private String code;
	private String checkinTime;
	private String checkoutTime;
	private int leftNum;
	
	
	public int getLeftNum() {
		return leftNum;
	}

	public void setLeftNum(int leftNum) {
		this.leftNum = leftNum;
	}

	public String getCheckinTime() {
		return checkinTime;
	}

	public void setCheckinTime(String checkinTime) {
		this.checkinTime = checkinTime;
	}

	public String getCheckoutTime() {
		return checkoutTime;
	}

	public void setCheckoutTime(String checkoutTime) {
		this.checkoutTime = checkoutTime;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public HotelOrder getOrder() {
		return order;
	}
	public void setOrder(HotelOrder order) {
		this.order = order;
	}
	public void setRoomInfo(RoomStyle roomInfo) {
		this.roomInfo = roomInfo;
	}
	public RoomStyle getRoomInfo() {
		return roomInfo;
	}
}
